package com.ruoyi.common.utils;



import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * Created On : 2024/3/30.
 * Author : xuliang
 * Description: SummarySheetHandler
 * <p>
 */
public class OutputHandler implements SheetWriteHandler {

    private String outputName;

    public OutputHandler(String outputName) {
        this.outputName = outputName;
    }


    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = writeSheetHolder.getSheet();

        // 获取当前工作表的名称
        String sheetName = outputName + "累计除税产值汇总表";

        // 设置标题
        Row row0 = sheet.createRow(0);
        row0.setHeight((short) 600);
        Cell title = row0.createCell(0);
        title.setCellValue(sheetName);
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        titleStyle.setFont(font);
        title.setCellStyle(titleStyle);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 9));

        // 设置制表日期
        Row row1 = sheet.createRow(1);
        row1.setHeight((short) 300);
        Cell dataCell = row1.createCell(0);
        LocalDate currentTime = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String formattedDate = currentTime.format(formatter);
        dataCell.setCellValue("制表日期：" + formattedDate);
        CellStyle dateStyle = workbook.createCellStyle();
        // 设置水平对齐方式为右对齐
        dateStyle.setAlignment(HorizontalAlignment.RIGHT);
        // 设置垂直对齐方式为中间对齐
        dateStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        dataCell.setCellStyle(dateStyle);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 9));

    }


}
